package fatboy.communication;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class FileLocation implements Location {
	private String filePath;
	private Object retValue;
	/**
	 * Basic constructor that takes the path to the file
	 * @param path basic path to the Object
	 */
	public FileLocation(String path){
		filePath = path;
	}
	/**
	 * gets the remote return
	 * @return the return value from method run on saved location
	 */
	public Object recieveReturn()throws IOException {
		return retValue;	
	}
	/**
	 * sends a command to the file to be executed
	 * @param o Command to be executed by the file 
	 */
	public void sendCommand(Command o) throws IOException{
		Command com = (Command) o;
		
		File file = new File(filePath);
	    if(!file.exists()){
	    	file.createNewFile();
	    }
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
		try {
			Object oldSaved = in.readObject();
//			com.setObjectInstance(oldSaved);
			retValue = com.excecute();
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
			out.writeObject(oldSaved);
			out.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
	/**
	 * doesn't do anything
	 */
	public void close() {
		
		
	}
	 
	public boolean isHere() {

		return false;
	}
}
